//-----------------------------------------------------------------------
// <copyright file="HoTroTrucTuyenDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using APPro.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace APPro.Data
{    
    public abstract class HoTroTrucTuyenDAOBase
    {
        #region Common methods
        public virtual HoTroTrucTuyen CreateHoTroTrucTuyenFromReader(IDataReader reader)
        {
            HoTroTrucTuyen item = new HoTroTrucTuyen();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("MaNguoiHoTro"))) item.MaNguoiHoTro = (int)reader["MaNguoiHoTro"];
                if (!reader.IsDBNull(reader.GetOrdinal("TenNguoiHoTro"))) item.TenNguoiHoTro = (string)reader["TenNguoiHoTro"];
                if (!reader.IsDBNull(reader.GetOrdinal("YahooAccount"))) item.YahooAccount = (string)reader["YahooAccount"];
                if (!reader.IsDBNull(reader.GetOrdinal("GmailAccount"))) item.GmailAccount = (string)reader["GmailAccount"];
                if (!reader.IsDBNull(reader.GetOrdinal("HotmailAccount"))) item.HotmailAccount = (string)reader["HotmailAccount"];
                if (!reader.IsDBNull(reader.GetOrdinal("SkyAccount"))) item.SkyAccount = (string)reader["SkyAccount"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateHoTroTrucTuyenFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateHoTroTrucTuyen methods
            
        public virtual void CreateHoTroTrucTuyen(HoTroTrucTuyen hoTroTrucTuyen)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHoTroTrucTuyenInsert");
                
				database.AddInParameter(dbCommand, "@MaNguoiHoTro", DbType.Int32, hoTroTrucTuyen.MaNguoiHoTro);
				database.AddInParameter(dbCommand, "@TenNguoiHoTro", DbType.String, hoTroTrucTuyen.TenNguoiHoTro);
				database.AddInParameter(dbCommand, "@YahooAccount", DbType.String, hoTroTrucTuyen.YahooAccount);
				database.AddInParameter(dbCommand, "@GmailAccount", DbType.String, hoTroTrucTuyen.GmailAccount);
				database.AddInParameter(dbCommand, "@HotmailAccount", DbType.String, hoTroTrucTuyen.HotmailAccount);
				database.AddInParameter(dbCommand, "@SkyAccount", DbType.String, hoTroTrucTuyen.SkyAccount);
				
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateHoTroTrucTuyenException, ex);
            }
        }

        #endregion
		
        #region UpdateHoTroTrucTuyen methods
        
        public virtual void UpdateHoTroTrucTuyen(HoTroTrucTuyen hoTroTrucTuyen)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHoTroTrucTuyenUpdate");            
                
                database.AddInParameter(dbCommand, "@MaNguoiHoTro", DbType.Int32, hoTroTrucTuyen.MaNguoiHoTro);
                database.AddInParameter(dbCommand, "@TenNguoiHoTro", DbType.String, hoTroTrucTuyen.TenNguoiHoTro);
                database.AddInParameter(dbCommand, "@YahooAccount", DbType.String, hoTroTrucTuyen.YahooAccount);
                database.AddInParameter(dbCommand, "@GmailAccount", DbType.String, hoTroTrucTuyen.GmailAccount);
                database.AddInParameter(dbCommand, "@HotmailAccount", DbType.String, hoTroTrucTuyen.HotmailAccount);
                database.AddInParameter(dbCommand, "@SkyAccount", DbType.String, hoTroTrucTuyen.SkyAccount);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateHoTroTrucTuyenException, ex);
            }
        }
        
        #endregion
		
        #region DeleteHoTroTrucTuyen methods
        public virtual void DeleteHoTroTrucTuyen(int maNguoiHoTro)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHoTroTrucTuyenDelete");
                
                database.AddInParameter(dbCommand, "@MaNguoiHoTro", DbType.Int32, maNguoiHoTro);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteHoTroTrucTuyenException, ex);
            }
        }

        #endregion
		
        #region GetHoTroTrucTuyen methods
        
        public virtual HoTroTrucTuyen GetHoTroTrucTuyen(int maNguoiHoTro)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHoTroTrucTuyenGet");
                
                database.AddInParameter(dbCommand, "@MaNguoiHoTro", DbType.Int32, maNguoiHoTro);
                
                HoTroTrucTuyen hoTroTrucTuyen =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        hoTroTrucTuyen = CreateHoTroTrucTuyenFromReader(reader);
                        reader.Close();
                    }
                }
                return hoTroTrucTuyen;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetHoTroTrucTuyenException, ex);
            }
        }
        
        #endregion
		
        #region GetHoTroTrucTuyenList methods
        public virtual HoTroTrucTuyenCollection GetHoTroTrucTuyenList(HoTroTrucTuyenColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spHoTroTrucTuyenGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                HoTroTrucTuyenCollection hoTroTrucTuyenCollection = new HoTroTrucTuyenCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        HoTroTrucTuyen hoTroTrucTuyen = CreateHoTroTrucTuyenFromReader(reader);
                        hoTroTrucTuyenCollection.Add(hoTroTrucTuyen);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return hoTroTrucTuyenCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetHoTroTrucTuyenListException, ex);
            }
        }
        
        public virtual HoTroTrucTuyenCollection GetHoTroTrucTuyenList(HoTroTrucTuyenColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetHoTroTrucTuyenList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}